package com.yaozheng.vocationaltraining.service.impl.userinfo;

import com.yaozheng.vocationaltraining.iview.BaseView;
import com.yaozheng.vocationaltraining.iview.IBaseView;
import com.yaozheng.vocationaltraining.iview.userinfo.IUpdatePasswordView;
import com.yaozheng.vocationaltraining.service.userinfo.UpdatePasswordService;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.ErrorUtils;
import com.yaozheng.vocationaltraining.utils.http.okhttp.OkHttpClientManager;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EBean;

import java.util.HashMap;
import java.util.Map;

/**
 * 修改密码
 */
@EBean
public class UpdatePasswordServiceImpl implements UpdatePasswordService {
    IUpdatePasswordView iUpdatePasswordView;

    @Override
    public void init(IUpdatePasswordView iUpdatePasswordView) {
        this.iUpdatePasswordView = iUpdatePasswordView;
    }

    @Background
    @Override
    public void updatePassword(String oldPassword, String newPassword, String confirmNewPassword) {
        Map<String, String> para = new HashMap<String, String>();
        para.put("oldPassword", oldPassword);
        para.put("newPassword", newPassword);
        para.put("confirmNewPassword", confirmNewPassword);
        try {
            OkHttpClientManager.getInstance().post(
                    Constants.ROOTURL + "/user/updatePassword",
                    iUpdatePasswordView.getToken(), para,
                    new OkHttpClientManager.IBaseViewCallback(new BaseView(iUpdatePasswordView) {

                        @Override
                        public void responseSuccess(JSONObject jsonObject) {
                            iUpdatePasswordView.updatePasswordSuccess(jsonObject);
                        }

                        @Override
                        public void responseError(String error) {
                            iUpdatePasswordView.updatePasswordError(error);
                        }


                    }));
        } catch (Exception e) {
            ErrorUtils.outErrorLog(e);
            if (iUpdatePasswordView.isResponseResult()) {
                iUpdatePasswordView
                        .updatePasswordError(ErrorUtils.SERVER_CONNECTION_ERROR);
            }
        }
    }
}
